package com.mobiusws.util;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Sheet;


public class ExportManage {
	private static Log logger = LogFactory.getLog(ExportManage.class);
	
	final String DATE_ALIAS="#";
	final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH");
	
	
	public void DBQueryReportDaily(String mcc_mnc, String strCountryCode, long maskBits, ExportReport reportStruct, String outputPath,ResultSet rs)
	{
		int[] retTotalRecordNum	= {0};
		float[] fSheetColumnWidthArray;
		try
		{
			retTotalRecordNum[0] = rs.getMetaData().getColumnCount();
			// CSV
			DBQueryReportDaily_CSV(mcc_mnc, strCountryCode, maskBits, reportStruct, outputPath, rs, retTotalRecordNum);

			// xlxs
			fSheetColumnWidthArray = DBQueryReportDaily_XLSX(mcc_mnc, strCountryCode, maskBits, reportStruct, outputPath, rs, retTotalRecordNum);

			// pdf
			DBQueryReportDaily_PDF(mcc_mnc, strCountryCode, maskBits, reportStruct, outputPath, rs, retTotalRecordNum, fSheetColumnWidthArray);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		

	}
	
	public void DBQueryReportDaily_CSV(String mcc_mnc, String strCountryCode, long maskBits, ExportReport reportStruct, String outPath, ResultSet rs, int[] retTotalRecordNum)
	{
		long time1, time2;

		int iColumnNum = reportStruct.getTableColumnsArray().length;

		CSVUtil csvUtil = new CSVUtil(outPath + "/" + mcc_mnc + "/csv/" + reportStruct.getReportName() + "_" + reportStruct.getReportDate() + ".csv");

		csvUtil.AddTitle(reportStruct);

		try
		{
			if (retTotalRecordNum[0]>0 )
			{
				// write the csv files
				time1 = System.currentTimeMillis();
				rs.beforeFirst();
				csvUtil.AddBody(rs, reportStruct, "0".compareTo(strCountryCode)==0?null:strCountryCode, maskBits, iColumnNum);
				time2 = System.currentTimeMillis();
				logger.info("	CSV Body time=" + ((time2 - time1) / 1000) + " seconds");
			}

		}
		catch (Exception e)
		{
			logger.error(e.getMessage());
			e.printStackTrace();
		}

		time1 = System.currentTimeMillis();
		csvUtil.CloseCSV();
		time2 = System.currentTimeMillis();
		logger.info("	CSV Save time=" + ((time2 - time1) / 1000) + " seconds");
		

	}

	public float[] DBQueryReportDaily_XLSX(String mcc_mnc, String strCountryCode, long maskBits, ExportReport reportStruct, String outPath, ResultSet rs, int[] retTotalRecordNum)
	{
		long time1, time2;

		int iColumnNum = reportStruct.getTableColumnsArray().length;

		// prepare the excel
		int iCurRow = 0;
		ExcelUtil excelUtil = new ExcelUtil(reportStruct.getReportName());

		excelUtil.CreateExcelSheet();

		excelUtil.AddLogo(iCurRow, iColumnNum);

		iCurRow++;

		if(reportStruct.getReportName().indexOf("weekly") >= 0 || reportStruct.getReportName().indexOf("hourly") >= 0 ) {
			excelUtil.AddTitleW(iCurRow, reportStruct);
			iCurRow++;
			iCurRow++;
			iCurRow++;
		} else {
			excelUtil.AddTitle(iCurRow, reportStruct);
			iCurRow++;
			iCurRow++;
			iCurRow++;
			//add by wilson
			iCurRow++;
			//add by wilson
		}

		try
		{
			if ( retTotalRecordNum[0]>0 )
			{
				time1 = System.currentTimeMillis();
				rs.beforeFirst();
				excelUtil.AddBody(iCurRow, rs, reportStruct, "0".compareTo(strCountryCode)==0?null:strCountryCode, maskBits, iColumnNum);
				time2 = System.currentTimeMillis();
				logger.info("	Excel Body time=" + ((time2 - time1) / 1000) + " seconds");

			}
		}
		catch (Exception e)
		{
			logger.error(e.getMessage());
			e.printStackTrace();
		}

		time1 = System.currentTimeMillis();
		excelUtil.AdjustAutoWidth(iColumnNum);
		time2 = System.currentTimeMillis();
		logger.info("	Excel Auto time=" + ((time2 - time1) / 1000) + " seconds");

		//get the style for pdf
		Sheet sheet = excelUtil.getWb().getSheet(reportStruct.getReportName());
		float[] fSheetColumnWidthArray = new float[iColumnNum];
		//System.out.println("Sheet Width start");
		for (int i = 0; i < iColumnNum; i++)
		{
			fSheetColumnWidthArray[i] = sheet.getColumnWidth(i);
			//System.out.print(fSheetColumnWidthArray[i] + "	");
		}
		//System.out.println("Sheet Width end");
		
		
		time1 = System.currentTimeMillis();
		excelUtil.SaveExcelSheet(outPath + "/" + mcc_mnc + "/xlsx/" + reportStruct.getReportName() + "_" + reportStruct.getReportDate() + ".xlsx");
		time2 = System.currentTimeMillis();
		logger.info("	Excel Save time=" + ((time2 - time1) / 1000) + " seconds");

		// Last step
		return fSheetColumnWidthArray;

		

	}

	public void DBQueryReportDaily_PDF(String mcc_mnc, String strCountryCode, long maskBits, ExportReport reportStruct, String outPath, ResultSet rs, int[] retTotalRecordNum, float[] fSheetColumnWidthArray)
	{
		long time1, time2;

		int iColumnNum = reportStruct.getTableColumnsArray().length;

		try
		{
			// write the pdf files
			time1 = System.currentTimeMillis();
			PdfUtil pdfUtil = new PdfUtil();
			rs.beforeFirst();
			pdfUtil.convert(reportStruct, "0".compareTo(strCountryCode)==0?null:strCountryCode, maskBits, rs, iColumnNum, retTotalRecordNum[0], fSheetColumnWidthArray, outPath + "/" + mcc_mnc + "/pdf/" + reportStruct.getReportName() + "_" + reportStruct.getReportDate() + ".pdf");
			time2 = System.currentTimeMillis();
			logger.info("	PDF Body time=" + ((time2 - time1) / 1000) + " seconds");

		}
		catch (Exception e)
		{
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}
	
	
	public void DBQueryReportDaily(String mcc_mnc, String strCountryCode, long maskBits, ExportReport reportStruct, String outputPath,List<List<Object>> ls)
	{
		int[] retTotalRecordNum	= {0};
		float[] fSheetColumnWidthArray;
		try
		{
			retTotalRecordNum[0] = ls.get(0).size();
			// CSV
			DBQueryReportDaily_CSV(mcc_mnc, strCountryCode, maskBits, reportStruct, outputPath, ls, retTotalRecordNum);

			// xlxs
			fSheetColumnWidthArray = DBQueryReportDaily_XLSX(mcc_mnc, strCountryCode, maskBits, reportStruct, outputPath, ls, retTotalRecordNum);

			// pdf
			DBQueryReportDaily_PDF(mcc_mnc, strCountryCode, maskBits, reportStruct, outputPath, ls, retTotalRecordNum, fSheetColumnWidthArray);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		

	}
	
	public void DBQueryReportDaily_CSV(String mcc_mnc, String strCountryCode, long maskBits, ExportReport reportStruct, String outPath, List<List<Object>> ls, int[] retTotalRecordNum)
	{
		long time1, time2;

		int iColumnNum = reportStruct.getTableColumnsArray().length;

		CSVUtil csvUtil = new CSVUtil(outPath + "/" + mcc_mnc + "/csv/" + reportStruct.getReportName() + "_" + reportStruct.getReportDate() + ".csv");

		csvUtil.AddTitle(reportStruct);

		try
		{
			if (retTotalRecordNum[0]>0 )
			{
				// write the csv files
				time1 = System.currentTimeMillis();
				csvUtil.AddBody(ls, reportStruct, "0".compareTo(strCountryCode)==0?null:strCountryCode, maskBits, iColumnNum);
				time2 = System.currentTimeMillis();
				logger.info("	CSV Body time=" + ((time2 - time1) / 1000) + " seconds");
			}

		}
		catch (Exception e)
		{
			logger.error(e.getMessage());
			e.printStackTrace();
		}

		time1 = System.currentTimeMillis();
		csvUtil.CloseCSV();
		time2 = System.currentTimeMillis();
		logger.info("	CSV Save time=" + ((time2 - time1) / 1000) + " seconds");
		

	}

	public float[] DBQueryReportDaily_XLSX(String mcc_mnc, String strCountryCode, long maskBits, ExportReport reportStruct, String outPath, List<List<Object>> ls, int[] retTotalRecordNum)
	{
		long time1, time2;

		int iColumnNum = reportStruct.getTableColumnsArray().length;

		// prepare the excel
		int iCurRow = 0;
		ExcelUtil excelUtil = new ExcelUtil(reportStruct.getReportName());

		excelUtil.CreateExcelSheet();

		excelUtil.AddLogo(iCurRow, iColumnNum);

		iCurRow++;
		if(reportStruct.getReportName().indexOf("weekly") >= 0 || reportStruct.getReportName().indexOf("Hourly") >= 0 ) {
			excelUtil.AddTitleW(iCurRow, reportStruct);
			iCurRow++;
			iCurRow++;
			iCurRow++;
		} else {
			excelUtil.AddTitle(iCurRow, reportStruct);
			iCurRow++;
			iCurRow++;
			iCurRow++;
			//add by wilson
			iCurRow++;
			//add by wilson
		}
		

		try
		{
			if ( retTotalRecordNum[0]>0 )
			{
				time1 = System.currentTimeMillis();
				excelUtil.AddBody(iCurRow, ls, reportStruct, "0".compareTo(strCountryCode)==0?null:strCountryCode, maskBits, iColumnNum);
				time2 = System.currentTimeMillis();
				logger.info("	Excel Body time=" + ((time2 - time1) / 1000) + " seconds");

			}
		}
		catch (Exception e)
		{
			logger.error(e.getMessage());
			e.printStackTrace();
		}

		time1 = System.currentTimeMillis();
		excelUtil.AdjustAutoWidth(iColumnNum);
		time2 = System.currentTimeMillis();
		logger.info("	Excel Auto time=" + ((time2 - time1) / 1000) + " seconds");

		//get the style for pdf
		Sheet sheet = excelUtil.getWb().getSheet(reportStruct.getReportName());
		float[] fSheetColumnWidthArray = new float[iColumnNum];
		//System.out.println("Sheet Width start");
		for (int i = 0; i < iColumnNum; i++)
		{
			fSheetColumnWidthArray[i] = sheet.getColumnWidth(i);
			//System.out.print(fSheetColumnWidthArray[i] + "	");
		}
		//System.out.println("Sheet Width end");
		
		
		time1 = System.currentTimeMillis();
		excelUtil.SaveExcelSheet(outPath + "/" + mcc_mnc + "/xlsx/" + reportStruct.getReportName() + "_" + reportStruct.getReportDate() + ".xlsx");
		time2 = System.currentTimeMillis();
		logger.info("	Excel Save time=" + ((time2 - time1) / 1000) + " seconds");

		// Last step
		return fSheetColumnWidthArray;

		

	}

	public void DBQueryReportDaily_PDF(String mcc_mnc, String strCountryCode, long maskBits, ExportReport reportStruct, String outPath, List<List<Object>> ls, int[] retTotalRecordNum, float[] fSheetColumnWidthArray)
	{
		long time1, time2;

		int iColumnNum = reportStruct.getTableColumnsArray().length;

		try
		{
			// write the pdf files
			time1 = System.currentTimeMillis();
			PdfUtil pdfUtil = new PdfUtil();
			pdfUtil.convert(reportStruct, "0".compareTo(strCountryCode)==0?null:strCountryCode, maskBits, ls, iColumnNum, retTotalRecordNum[0], fSheetColumnWidthArray, outPath + "/" + mcc_mnc + "/pdf/" + reportStruct.getReportName() + "_" + reportStruct.getReportDate() + ".pdf");
			time2 = System.currentTimeMillis();
			logger.info("	PDF Body time=" + ((time2 - time1) / 1000) + " seconds");

		}
		catch (Exception e)
		{
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}
	
	
	

}
